/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JDialogAbstract.java
 *
 * Created on 23 Sep 10, 0:50:47
 */

package Riset1;
import com.adobe.acrobat.*;
import java.awt.*;
import java.io.*;
import amd.database.ConnectDB;
import amd.database.DbDocument;
import amd.database.DbDocumentSimilarity;
import java.util.ArrayList;
import org.pdfbox.*;

/**
 *
 * @author Presario
 */
public class JDialogAbstract extends javax.swing.JDialog {

    /** Creates new form JDialogAbstract */
    public JDialogAbstract(java.awt.Frame parent, boolean modal, String s) {
        super(parent, modal);
        conn=null;
        initComponents();
        try {
            conn = new ConnectDB("jdbc:mysql://localhost:3306/db_amd_lsa", "root", "");
        } catch (Exception ex) {
            System.out.println(ex);
            System.exit(0);
        }
        FillInFrame(conn,s);
    }
    public void FillInFrame(ConnectDB conn, String s) {
        DbDocument dbDoc=new DbDocument();
        try{
            ArrayList<String> isiDocs= dbDoc.getDataDocument(conn, s);
            jTextFieldIdDoc.setText(isiDocs.get(0));
            jTextAreaTitle.setText(isiDocs.get(1));
            textAreaAbstract.setText("Abstract\n"+isiDocs.get(2).toString());
            path=isiDocs.get(3).toString();
        }catch(Exception ex){
            System.out.println(ex.toString());
        }
        DbDocumentSimilarity dbDocSimilar = new DbDocumentSimilarity();
        try{
            ArrayList<ArrayList<String>> similarDocs= dbDocSimilar.getDocumentSimilar(conn, s);
            for(int i=0; i<similarDocs.size(); i++) {
                ArrayList<String> tmp= similarDocs.get(i);
                for(int j=0; j<tmp.size()-1; j++)
                    jTableSimilar.setValueAt(tmp.get(j), i, j);
                pathSimilar[i]=tmp.get(3).toString();
            }
        }catch(Exception ex){
            System.out.println(ex.toString());
        }
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        textAreaAbstract = new java.awt.TextArea();
        jButtonBaca = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTableSimilar = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jLabelDokuman = new javax.swing.JLabel();
        jTextFieldIdDoc = new javax.swing.JTextField();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextAreaTitle = new javax.swing.JTextArea();
        jLabelIdDoc = new javax.swing.JLabel();
        jLabelJudul = new javax.swing.JLabel();
        jLabelAbstract = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        textAreaAbstract.setEditable(false);

        jButtonBaca.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jButtonBaca.setText("Baca Isi Dokumen");
        jButtonBaca.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonBacaActionPerformed(evt);
            }
        });

        jTableSimilar.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "IdDoc", "Judul"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jTableSimilar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTableSimilarMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTableSimilar);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel1.setText("Similar Documents:");

        jLabelDokuman.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabelDokuman.setText("Data Dokumen");

        jTextFieldIdDoc.setEditable(false);

        jTextAreaTitle.setColumns(20);
        jTextAreaTitle.setEditable(false);
        jTextAreaTitle.setRows(5);
        jScrollPane2.setViewportView(jTextAreaTitle);

        jLabelIdDoc.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabelIdDoc.setText("Id");

        jLabelJudul.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabelJudul.setText("Judul");

        jLabelAbstract.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabelAbstract.setText("Abstract");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jTextFieldIdDoc, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabelIdDoc))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 334, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabelJudul)))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 419, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(textAreaAbstract, javax.swing.GroupLayout.DEFAULT_SIZE, 434, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jButtonBaca)
                                .addGap(27, 27, 27))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 217, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 217, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jLabelAbstract))
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabelDokuman)
                        .addGap(173, 173, 173))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addComponent(jLabelDokuman)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelIdDoc)
                    .addComponent(jLabelJudul))
                .addGap(4, 4, 4)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextFieldIdDoc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(16, 16, 16)
                .addComponent(jLabelAbstract)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(textAreaAbstract, javax.swing.GroupLayout.PREFERRED_SIZE, 191, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonBaca)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 221, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonBacaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonBacaActionPerformed
        // TODO add your handling code here:
        System.out.println(path);
//        path=path.replace('/', '\');
        System.out.println(path);
//        Context.enter(arg0);
//        Window own = this.getOwner();
        this.dispose();
        final Thread t=new Thread();
        t.setName("bacapdf");
        t.start();
//        own.
        Frame frame = new Frame("JTK-PDF-Viewer");

        frame.setFocusable(true);
        frame.setLayout(new BorderLayout());
        frame.setEnabled(true);
        try{
            Viewer viewer = new Viewer();
            frame.add(viewer, BorderLayout.CENTER);
            InputStream input = new FileInputStream (new File(path));
//            viewer.setFont(Font.getFont("arial"));
            viewer.setDocumentInputStream(input);
//            viewer.setFont(Font.getFont("arial"));
            viewer.activate();
            frame.setSize(400, 500);
            frame.pack();
            viewer.execMenuItem(ViewerCommand.FitWidth_K);
            frame.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        e.getWindow().dispose();
                        t.stop();
                    }
                });
            frame.setVisible(true);
        } catch(Exception ex) {
            System.out.print(ex.toString());
        }



}//GEN-LAST:event_jButtonBacaActionPerformed

    private void jTableSimilarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableSimilarMouseClicked
        // TODO add your handling code here:
        int baris=jTableSimilar.getSelectedRow();
        Object ob=jTableSimilar.getValueAt(baris, 0);
        if(ob != null) {
            final String obString=ob.toString();
            FillInFrame(conn, obString);
        }

    }//GEN-LAST:event_jTableSimilarMouseClicked

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                JDialogAbstract dialog = new JDialogAbstract(new javax.swing.JFrame(), true);
//                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
//                    public void windowClosing(java.awt.event.WindowEvent e) {
//                        System.exit(0);
//                    }
//                });
//                dialog.setVisible(true);
//            }
//        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonBaca;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabelAbstract;
    private javax.swing.JLabel jLabelDokuman;
    private javax.swing.JLabel jLabelIdDoc;
    private javax.swing.JLabel jLabelJudul;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTableSimilar;
    private javax.swing.JTextArea jTextAreaTitle;
    private javax.swing.JTextField jTextFieldIdDoc;
    private java.awt.TextArea textAreaAbstract;
    // End of variables declaration//GEN-END:variables
    private String path;
    private String pathSimilar[];
    ConnectDB conn;
}
